<template>
  <div>
    <el-row type="flex" justify="end">
      <el-select v-model="currentYear" size="small" style="width: 120px" @change="dateChange">
        <el-option v-for="item in yearList" :key="item" :label="item" :value="item">{{ item }}</el-option>
      </el-select>
      <el-select v-model="currentMonth" size="small" style="width: 120px;margin-left:10px" @change="dateChange">
        <el-option v-for="item in 12" :key="item" :label="item" :value="item">{{ item }}</el-option>
      </el-select>
    </el-row>
    <el-calendar v-model="currentDate">
      <template v-slot:dateCell="{ date, data }" class="content">
        <div class="date-content">
          <span class="text"> {{ data.day | getDay }}</span>
          <span v-if="isWeekend(date)" class="rest">休</span>
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
export default {
  filters: {
    getDay(val) {
      const valDate = val.split('-')[2]
      return valDate < 10 ? valDate.substr(1) : valDate
    }
  },
  props: {
    startDate: {
      type: Date,
      default: () => new Date()
    }
  },
  data() {
    return {
      currentMonth: null, // 当前月份
      currentYear: null, // 当前年份
      currentDate: null,
      yearList: []
    }
  },
  created() {
    const date = new Date()
    // 1. 页面一进来就拿到开始时间(就是当时的时间) 计算当年年份 + 当前的月份
    this.currentMonth = date.getMonth() + 1
    this.currentYear = date.getFullYear()
    // const begin = this.currentYear
    // const arr = []
    // for (let i = 0; i < 11; i++) {
    //   arr.push(begin - 5 + i)
    // }
    // 2. 计算当年年份列表
    this.yearList = Array.from({ length: 11 }, (val, index) => this.currentYear - 5 + index)
    //
    this.currentDate = new Date()
  },
  methods: {
    dateChange() {
      // 这是在任意一个下拉窗改变的时候触发
      // 拿到当前年，当前月，组成一个新的日期
      // 交给日历组件更新渲染
      // new Date(日期字符串) 可以生成指定某一天的日期
      this.currentDate = new Date(`${this.currentYear}-${this.currentMonth}`)
    },
    isWeekend(val) {
      return val.getDay() === 0 || val.getDay() === 6
    }
  }
}
</script>

<style lang="scss" scoped>

 ::v-deep .el-calendar-day {
  height:  auto;
 }
 ::v-deep .el-calendar-table__row td,::v-deep .el-calendar-table tr td:first-child,  ::v-deep .el-calendar-table__row td.prev{
  border:none;
 }
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text{
  width: 20px;
  height: 20px;
  line-height: 20px;
 display: inline-block;

}
 ::v-deep .el-calendar-table td.is-selected .text{
   background: #409eff;
   color: #fff;
   border-radius: 50%;
 }
 ::v-deep .el-calendar__header {
   display: none
 }
</style>
